package gdonly.top.signin.widgets;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import gdonly.top.signin.R;
import gdonly.top.signin.business.Application;
import gdonly.top.signin.business.QueryStuFromCourseActivity;
import gdonly.top.signin.utils.DatabaseQuery;


/**
 * Created by zhuzhuxia on 16/12/26.
 */
public class ShowRecordsByCourseAdapter extends RecyclerView.Adapter<ViewHolder2> {
    private List<HashMap<String, Object>> mData;
    private Activity context;

    private String courseId;

    public ShowRecordsByCourseAdapter(List<HashMap<String, Object>> list, Activity context, String courseId) {
        mData = list;
        this.courseId = courseId;
        this.context = context;
    }

    @Override
    public ViewHolder2 onCreateViewHolder(ViewGroup parent, int viewType) {
        View linearLayout = LayoutInflater.from(context).inflate(R.layout.item_resultsignbycourse, parent, false);

        ViewHolder2 viewHolder=new ViewHolder2(linearLayout,context);

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ViewHolder2 holder, int position) {


        if(mData!=null) {

            holder.shouldTv.setText(String.valueOf(mData.get(position).get("should")));
            holder.actualTv.setText(String.valueOf( mData.get(position).get("actual")));
            final String date = (String) mData.get(position).get("date");

            holder.dateTv.setText(date);
            holder.rateTv.setText((String)mData.get(position).get("rate"));
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String classId = ((Application)context.getApplication()).getThisUser().getClassId();
                    Log.e("TAG", "onclick......");
                    String result = DatabaseQuery.getStusInCourseSign(classId, date, courseId, context);

                    Intent intent = new Intent(context, QueryStuFromCourseActivity.class);
                    intent.putExtra("result",result);
                    context.startActivity(intent);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}

class ViewHolder2 extends RecyclerView.ViewHolder {

        public TextView dateTv;
        public TextView rateTv;
        public TextView actualTv;
        public TextView shouldTv;
        private Activity context;
        public CardView cardView;

        public ViewHolder2(View itemView, Activity context) {
            super(itemView);
            this.context = context;

            rateTv = (TextView) itemView.findViewById(R.id.rate_tv);
            actualTv = (TextView) itemView.findViewById(R.id.actual_tv);
            shouldTv = (TextView) itemView.findViewById(R.id.should_tv);
            dateTv = (TextView) itemView.findViewById(R.id.dateinresult_tv);
            cardView = (CardView) itemView.findViewById(R.id.card_view);

        }

}

